import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    redirect: '/home/index',
    children: [
      {
        path: "index",
        component: () => import('../views/IndexView.vue')
      },
      {
        path: "addview",
        component: () => import('../views/AddView.vue')
      },
      {
        path: "userlist",
        component: () => import('../views/UserList.vue')
      },
      {
        path: "lookview",
        component: () => import('../views/LookView.vue')
      },
    ]
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/LoginView.vue')
  },
]



const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router

// 全局路由前置守卫   (to, from, next)前面用了，后面用了，中间不用没事
router.beforeEach((to, from, next) => {

  if (to.path == '/login') {
    next()
  } else if (!localStorage.getItem('token')) {   //localStorage.getItem('token')没有令牌了 == undefined  == false
    next('/login')
  } else {
    next()
  }
})